package net.com.hydroPK.model.skills.fletching;

import net.com.hydroPK.Engine;
import net.com.hydroPK.model.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Fletching {
    private Timer timer;
    private int counter = 0, times = 0;                                                                                   //  {{item1, item2, screenID}, {show items to make},{ req lvl},{ given xp}}
    private static final int[][][] FletchingId = new int[][][]{{{946, 1511, 0}, {50, 48, 52, 9440}, {10, 5, 0, 9}, {250, 450, 100, 120}}, {{946, 1521, 1}, {54, 56, 9442}, {20, 25, 24}, {1000, 1500, 320}}, {{946, 1519, 2}, {60, 58, 9444}, {35, 40, 39}, {1500, 1670, 450}}, {{946, 1517, 3}, {64, 62, 9446}, {50, 55, 54}, {2500, 2870, 650}}, {{946, 1515, 4}, {68, 66, 9448}, {65, 70, 69}, {3250, 3500, 1100}}, {{946, 1513, 5}, {70, 72}, {80, 85}, {5000, 6500}}};
    private static final int Buttons[][][][] = new int[][][][]{

/*normal*/  {{{9, 1}, {8, 5}, {7, 10}, {10, -1}}, {{13, 1}, {12, 5}, {11, 10}, {15, -1}}, {{17, 1}, {16, 5}, {15, 10}, {19, -1}}, {{21, 1}, {20, 5}, {19, 10}, {25, -1}}},
/*oak*/     {{{8, 1}, {7, 5}, {6, 10}, {25, -1}}, {{12, 1}, {11, 5}, {10, 10}, {32, -1}}, {{16, 1}, {15, 5}, {14, 10}, {46, -1}}},
/*willow*/  {{{9, 1}, {8, 5}, {7, 10}, {50, -1}}, {{13, 1}, {12, 5}, {11, 10}, {60, -1}}, {{17, 1}, {16, 5}, {15, 10}, {70, -1}}, {{21, 1}, {20, 5}, {19, 10}, {80, -1}}},
/*maple*/   {{{9, 1}, {8, 5}, {7, 10}, {50, -1}}, {{13, 1}, {12, 5}, {11, 10}, {80, -1}}, {{17, 1}, {16, 5}, {15, 10}, {70, -1}}, {{21, 1}, {20, 5}, {19, 10}, {70, -1}}},
/*yew*/     {{{9, 1}, {8, 5}, {7, 10}, {60, -1}}, {{13, 1}, {12, 5}, {11, 10}, {85, -1}}, {{17, 1}, {16, 5}, {15, 10}, {75, -1}}, {{21, 1}, {20, 5}, {19, 10}, {75, -1}}},
/*magic*/   {{{7, 1}, {6, 5}, {5, 10}, {100, -1}}, {{11, 1}, {10, 5}, {9, 10}, {150, -1}}}};

    public static void fletching(Player p, int item, int item2) {
        int id[][] = check(new int[]{item, item2});
        if (id[0][0] == -1)
            return;

        FletchingInterface.OptionInterface(p, id[1]);
        p.isFletching = true;
        p.fletchingScreenID = id[0][0];
    }

    private static int[][] check(int[] item) {
        for (int[][] aFletchingId : FletchingId) {
            if ((aFletchingId[0][1] == item[0] || aFletchingId[0][1] == item[1])
                    && (aFletchingId[0][0] == item[0] || aFletchingId[0][0] == item[1]))
                return new int[][]{{aFletchingId[0][2]}, aFletchingId[1]};
        }
        return new int[][]{{-1}, {-1}};
    }

    private static boolean HasItems(Player p, int item, int item2) {
        return (Engine.playerItems.invItemCount(p, item) > 0)
                && (Engine.playerItems.invItemCount(p, item2) > 0);
    }

    public static void fletchingGetItemIdAndAmount(Player p, int buttonId) {
        if (!p.isFletching)
            return;
        if (p.fletchingScreenID > 5 && p.fletchingScreenID <= -1)
            return;
        for (int i = 0; i < Buttons[p.fletchingScreenID].length; i++)
            for (int j = 0; j < Buttons[p.fletchingScreenID][i].length; j++)
                if (buttonId == Buttons[p.fletchingScreenID][i][j][0]) {
                    if (p.skillLvl[9] < FletchingId[p.fletchingScreenID][2][i]) {
                        p.getActionSender().sendMessage(p, "You need an Fletching level of " + FletchingId[p.fletchingScreenID][2][i] + " to fletch " + Engine.items.getItemName(FletchingId[p.fletchingScreenID][1][i]) + ".");
                        return;
                    }
                    if (Buttons[p.fletchingScreenID][i][j][1] == -1) {
                        p.getActionSender().runScript(p, 108, new Object[]{"How many do you want to make:"}, "s");
                        p.nextInput = 15;
                        p.iteminfo = new int[]{FletchingId[p.fletchingScreenID][1][i], FletchingId[p.fletchingScreenID][0][1], FletchingId[p.fletchingScreenID][0][0], FletchingId[p.fletchingScreenID][3][i]};
                    } else {
                        new Fletching(p, new int[]{FletchingId[p.fletchingScreenID][1][i], FletchingId[p.fletchingScreenID][0][1], FletchingId[p.fletchingScreenID][0][0], FletchingId[p.fletchingScreenID][3][i]}, Buttons[p.fletchingScreenID][i][j][1]);
                    }

                }
    }

    public static void fletchingX(Player p, int amount) {
        new Fletching(p, p.iteminfo, amount);
    }

    public Fletching(final Player p, final int[] itemInfo, final int Amount) {
        if (p.isBussy || p.isInterruptable) {
            return;
        }
        p.isFletching = true;
        p.isInterruptable = true;
        p.isBussy = true;
        times = Amount;
        timer = new Timer(1000, new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (!p.isInterruptable || !p.isBussy || p.disconnected[0] ||
                        !p.isFletching || times == 0 || !HasItems(p, itemInfo[2], itemInfo[1])) {
                    timer.stop();
                    timer.setRepeats(false);
                    p.isFletching = false;
                    p.isInterruptable = false;
                    p.isBussy = false;
                    p.fletchingScreenID = -1;
                    counter = -1;
                } else {
                    Engine.playerMovement.resetWalkingQueue(p);
                    p.requestAnim(1248, 0);
                }

                if (counter == 0) {
                    Engine.playerItems.deleteItem(p, itemInfo[1], 1);
                    Engine.playerItems.addItem(p, itemInfo[0], itemInfo[0] == 52 ? 15 : 1);
                    p.appendExperience(itemInfo[3], 9);
                    counter = 2;

                    times--;
                }
                counter--;
            }
        });
        timer.setRepeats(true);
        timer.start();

    }
}
